package controllers

import (
	"2301_qidian/cache"
	"2301_qidian/models"
	"context"
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
)

var ctx = context.Background()

type NovelController struct {
	beego.Controller
}

func (c *NovelController) Add() {
	title := c.GetString("title")
	author := c.GetString("author")
	content := c.GetString("content")
	if title == "" || author == "" || content == "" {
		c.Data["json"] = map[string]interface{}{
			"code": 0,
			"msg":  "添加内容不能为空",
		}
		c.ServeJSON()
		return
	}
	var novel models.Novel

	novel.Title = title
	novel.Author = author
	novel.Content = content
	err := novel.Add()
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "添加失败",
		}
		c.ServeJSON()
		return
	}
	novels, _ := novel.List()
	jsonData, _ := json.Marshal(novels)
	cache.Rdb.Set(ctx, "novel", jsonData, 0)
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "添加成功",
	}
	c.ServeJSON()
}
func (c *NovelController) List() {
	cacheData := cache.Rdb.Get(ctx, "novel_list").Val()
	var novels []models.Novel
	if cacheData != "" {
		models.DB.Find(&novels)
		jsonData, _ := json.Marshal(novels)
		cache.Rdb.Set(ctx, "novel", jsonData, 0)
	} else {
		json.Unmarshal([]byte(cacheData), &novels)
	}
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "获取成功",
	}
	c.ServeJSON()
	return
}
func (c *NovelController) Del() {
	id, _ := c.GetInt("id")
	if id < 1 {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "参数错误",
		}
		c.ServeJSON()
		return
	}
	var novel models.Novel
	novel.ID = uint(id)
	err := novel.Del()
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "删除失败",
		}
		c.ServeJSON()
		return
	}
	novels, _ := novel.List()
	jsonData, _ := json.Marshal(novels)
	cache.Rdb.Set(ctx, "novel", jsonData, 0)
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "删除成功",
	}
	c.ServeJSON()
	return
}
func (c *NovelController) Update() {
	id, _ := c.GetInt("id")
	if id < 1 {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "参数错误",
		}
		c.ServeJSON()
		return
	}
	var novel models.Novel
	novel.ID = uint(id)
	novel.Title = c.GetString("title")
	novel.Author = c.GetString("author")
	novel.Content = c.GetString("content")
	err := novel.Update()
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 0,
			"msg":  "更新失败",
		}
		c.ServeJSON()
		return
	}
	novels, _ := novel.List()
	jsonData, _ := json.Marshal(novels)
	cache.Rdb.Set(ctx, "novel", jsonData, 0)
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "更新成功",
	}
	c.ServeJSON()
	return
}
func (c *NovelController) Detail() {
	id, _ := c.GetInt("id")
	if id < 1 {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "参数错误",
		}
		c.ServeJSON()
		return
	}
	var novel models.Novel
	novel.ID = uint(id)
	err := novel.Detail()
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 0,
			"msg":  "获取失败",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "获取成功",
	}
	c.ServeJSON()
	return
}
func (c *NovelController) UpdateStatus() {
	id, _ := c.GetInt("id")
	if id < 1 {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "参数错误",
		}
		c.ServeJSON()
		return
	}
	status, _ := c.GetInt("status")
	if status != 1 && status != 0 {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "参数错误",
		}
		c.ServeJSON()
		return
	}
	var novel models.Novel
	novel.ID = uint(id)
	novel.Status = status
	err := novel.Update()
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 0,
			"msg":  "更新失败",
		}
		c.ServeJSON()
		return
	}
	novels, _ := novel.List()
	jsonData, _ := json.Marshal(novels)
	cache.Rdb.Set(ctx, "novel", jsonData, 0)

	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "更新成功",
	}
	c.ServeJSON()
	return
}
